package main

import (
	"fmt"
	"os"
	"path"
	"strings"
)

const rootPath1 = "/root/movie1/水月洞天"
const rootPath2 = "/root/movie1/灵镜传奇"

func rename(dirPath string) {
	newName1 := strings.Split(dirPath, "/")[len(strings.Split(dirPath, "/"))-1]
	readDir, err := os.ReadDir(dirPath)
	if err != nil {
		panic(err)
	}
	for _, entry := range readDir {
		if !entry.IsDir() {
			continue
		}
		subDirName := entry.Name()
		subDirPath := path.Join(dirPath, subDirName)
		readSubDir, err := os.ReadDir(subDirPath)
		if err != nil {
			panic(err)
		}
		for _, dirEntry := range readSubDir {
			originFileName := dirEntry.Name()
			filename := dirEntry.Name()
			if !strings.HasPrefix(filename, "VTS") {
				continue
			}
			if !strings.HasSuffix(filename, ".mp4") {
				continue
			}
			// 去后缀
			filename = strings.TrimSuffix(filename, ".mp4")
			filename = strings.TrimSuffix(filename, ".VOB")
			// 下划线分割
			split := strings.Split(filename, "_")
			index := split[len(split)-1]
			if index == "0" {
				continue
			}
			newName := fmt.Sprintf("《%s》%s集P%s.mp4", newName1, subDirName, index)
			_ = os.Rename(path.Join(subDirPath, originFileName), path.Join(subDirPath, newName))
		}
	}
}

func main() {
	rename(rootPath1)
	rename(rootPath2)
}
